import 'package:ble_measure_plugin/library_ble_measure.dart';
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:ble_measure_plugin/ble_measure_plugin.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

  @override
  void initState() {
    super.initState();
    BleMeasurePlugin.instance.addTemMeasureListener(temHandeler: (temp) async{
      setState(() {
        text = temp.temValue??"";
      });
    },temErrorHandler: (error) async{
      setState(() {
        text = error;
      });
    });
    BleMeasurePlugin.instance.addPressureMeasureListener(
        pressureMeasuringHandler: (data) async {
          setState((){
            text = data;
          });
        },
        pressureResultHandler: (data) async {
          setState((){
            text = "${data.lowPressure},${data.highPressure},${data.pulse??""}";
          });
        },
        errorHandler: (error) async {
          setState((){
            text = error;
          });
        });
  }

  String text = "";

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          children: [
            Text(text),
            ElevatedButton(
                onPressed: () async {
                  var options = BodyFatDataOption(
                      type: 0,
                      sex: 1,
                      age: 25,
                      weight: 65,
                      height: 170,
                      adc: 500);
                  var data = await BleMeasurePlugin.getBodyFatData(options);
                  setState(() {
                    text = data!.bmi!.toString();
                  });
                },
                child: Text('getFatData')),
            ElevatedButton(
                onPressed: () {
                  ConnectDeviceUtils.initMeasureDevice();

                },
                child: Text('健康设备连接'))
          ],
        ),
      ),
    );
  }

}
